<?php
include_once("includes/common.php");
include_once("class/User.class.php");
include_once("class/Action.class.php");
include_once("class/Comments.class.php");
include_once("class/Explore.class.php");
require_once("islogin.php");

$pageName = "通知";
if ($islogin!=1){
    header("location:login.php");
    exit;
}
if($_GET['tab']=='like'){
    $action = new Action();
    $likeArray = json_decode($action->getNotifyLike($user_info['message']['user_id'],1,500),true);
    //var_dump($likeArray);
}else if($_GET['tab']=='fans'){
    $fansArray = json_decode($user->getUserFans($user_info['message']['user_id'],1,500),true);
    //var_dump($fansArray);
    $explore = new Explore();
}else{
    $comment = new Comments();
    $commentsArray = json_decode($comment->getNotifyComments($user_info['message']['user_id'],1,500),true);
    if ($_GET['delc']){
        $cid = $_GET['delc'];
        if (!isUid($cid)){
            exit;
        }
        $cdelres = json_decode($comment->deleteComments($user_info['message']['user_id'],$cid),true);
        if ($cdelres['status']==200){
            header("location:notifiy.php");
            exit;
        }else{
            echo "<script>alert('删除失败！')</script>";
            exit;
        }

    }
}
?>
<!DOCTYPE html>
<html>
<?php require_once('head.php'); ?>
	<body>
    <?php require_once('header.php'); ?>
		<div class="container-fluid content">
			<div class="row">
                <?php require_once('menu.php'); ?>
				<div class="col watercon">
					<div class="row justify-content-center notify_content">
						<div class="col">
							<div class="notify_head">
								<button class="n_tabBtn" onclick="window.location.href='/notifiy.php'" <?php if($_GET['tab']!='like'&&$_GET['tab']!='fans'){echo 'id="tabActive"';}?>>收到评论</button>
								<button class="n_tabBtn" onclick="window.location.href='?tab=like'" <?php if($_GET['tab']=='like'){echo 'id="tabActive"';}?>>赞和收藏</button>
								<button class="n_tabBtn" onclick="window.location.href='?tab=fans'" <?php if($_GET['tab']=='fans'){echo 'id="tabActive"';}?>>新增关注</button>
							</div>
                            <?php
                            if($_GET['tab']=='like'){?>
                                <!--like通知条start-->
                                <div class="notify_comments">
                                    <?php for ($lnum=0;$lnum<count($likeArray);$lnum++){?>
                                        <!--like通知条每行start-->
                                        <div class="row">
                                            <div class="col-1 notify_uimgdiv" onclick="window.location.href='/profile.php?id=<?php echo $likeArray[$lnum]['user_id'];?>'"><img src="<?php echo !$likeArray[$lnum]["avatar"] ? "/data/avatar/default.jpg" : $likeArray[$lnum]["avatar"];?>" alt="" class="notify_uavatar"/></div>
                                            <div class="col comments_hr">
                                                <div class="n_uname" onclick="window.location.href='/profile.php?id=<?php echo $likeArray[$lnum]['user_id'];?>'"><?php echo $likeArray[$lnum]['nickname'];?></div>
                                                <div class="n_commentstype n_liketab_text"> <?php echo formatNoteTime($likeArray[$lnum]['action_time']);?> <?php echo ($likeArray[$lnum]['action_type'] == 0 ? '点赞' : '收藏'); ?>了你的作品:《<a class="comments_note_title" href="/explore.php?id=<?php echo $likeArray[$lnum]['note_id'];?>"><?php echo $likeArray[$lnum]['title'];?></a>》</div>
                                                <div class="n_noteinfo n_liketab_ctext" onclick="window.location.href='/explore.php?id=<?php echo $likeArray[$lnum]['note_id'];?>'"><?php echo mb_substr($likeArray[$lnum]['ncontent'], 0, 46, 'UTF-8');?>...</div>

                                            </div>
                                            <div class="col-1 comments_hr" onclick="window.location.href='/explore.php?id=<?php echo $likeArray[$lnum]['note_id'];?>'"><img src="<?php echo getFirstValidImage($likeArray[$lnum]['image']);?>" class="notify_notepic"/></div>
                                        </div>
                                        <!--like通知条每行end-->
                                    <?php }?>
                                </div>
                                <!--like通知条end-->
                            <?php }else if($_GET['tab']=='fans'){ ?>
                                <!--fans通知条start-->
                                <div class="notify_comments">
                                    <?php for ($cnum=0;$cnum<count($fansArray);$cnum++){ $commentsArray = $fansArray;$isfollow = $explore->getIsFans($commentsArray[$cnum]['fans_id'],$user_info['message']['user_id']);?>
                                        <!--fans通知条每行start-->
                                        <div class="row">
                                            <div class="col-1 notify_uimgdiv" onclick="window.location.href='/profile.php?id=<?php echo $commentsArray[$cnum]['fans_id'];?>'"><img src="<?php echo !$commentsArray[$cnum]["avatar"] ? "/data/avatar/default.jpg" : $commentsArray[$cnum]["avatar"];?>" alt="" class="notify_uavatar"/></div>
                                            <div class="col comments_hr">
                                                <div class="n_uname" onclick="window.location.href='/profile.php?id=<?php echo $commentsArray[$cnum]['fans_id'];?>'"><?php echo $commentsArray[$cnum]['nickname'];?></div>
                                                <div class="n_commentstype n_fanstab_text">Ta关注了你，期待你的回关 <?php echo formatNoteTime($commentsArray[$cnum]['fans_time']);?></div>

                                            </div>
                                            <div class="col-2 comments_hr">
                                                <div class="col text-end follow_btn" style="padding-top: 10px">
                                                    <?php if($isfollow){?>
                                                        <button class="follows_btn" id="followButton" data-fans-id="<?php echo $user_info['message']['user_id'];?>" data-uid="<?php echo $commentsArray[$cnum]['fans_id'];?>">已关注</button>
                                                    <?php }else{?>
                                                        <button class="becomefans" id="followButton" data-fans-id="<?php echo $user_info['message']['user_id'];?>" data-uid="<?php echo $commentsArray[$cnum]['fans_id'];?>">关注</button>
                                                    <?php }?>
                                                </div>
                                            </div>
                                        </div>
                                        <!--fans通知条每行end-->
                                    <?php }?>
                                </div>
                                <!--fans通知条end-->
                            <?php }else{?>
                                <!--评论通知条start-->
                                <div class="notify_comments">
                                    <?php for ($cnum=0;$cnum<count($commentsArray);$cnum++){?>
                                        <!--评论通知条每行start-->
                                        <div class="row">
                                            <div class="col-1 notify_uimgdiv" onclick="window.location.href='/profile.php?id=<?php echo $commentsArray[$cnum]['user_id'];?>'"><img src="<?php echo !$commentsArray[$cnum]["avatar"] ? "/data/avatar/default.jpg" : $commentsArray[$cnum]["avatar"];?>" alt="" class="notify_uavatar"/></div>
                                            <div class="col comments_hr">
                                                <div class="n_uname" onclick="window.location.href='/profile.php?id=<?php echo $commentsArray[$cnum]['user_id'];?>'"><?php echo $commentsArray[$cnum]['nickname'];?></div>
                                                <div class="n_commentstype"> <?php echo formatNoteTime($commentsArray[$cnum]['comments_time']);?> 回复了你的作品:《<a class="comments_note_title" href="/explore.php?id=<?php echo $commentsArray[$cnum]['note_id'];?>"><?php echo $commentsArray[$cnum]['title'];?></a>》</div>
                                                <div class="n_ucomments" onclick="window.location.href='/explore.php?id=<?php echo $commentsArray[$cnum]['note_id'];?>'"><?php echo $commentsArray[$cnum]['content'];?></div>
                                                <div class="n_noteinfo" onclick="window.location.href='/explore.php?id=<?php echo $commentsArray[$cnum]['note_id'];?>'"><?php echo mb_substr($commentsArray[$cnum]['ncontent'], 0, 46, 'UTF-8');?>...</div>
                                                <div class="n_delete" onclick="window.location.href='?delc=<?php echo $commentsArray[$cnum]['comments_id'];?>'"><img src="static/img/garbage.svg" />删除</div>
                                            </div>
                                            <div class="col-1 comments_hr" onclick="window.location.href='/explore.php?id=<?php echo $commentsArray[$cnum]['note_id'];?>'"><img src="<?php echo getFirstValidImage($commentsArray[$cnum]['image']);?>" class="notify_notepic"/></div>
                                        </div>
                                        <!--评论通知条每行end-->
                                    <?php }?>
                                </div>
                                <!--评论通知条end-->
                            <?php }?>


							
							<div class="notify_end">- THE END -</div>
						</div>
					</div>

				</div>
			</div>
		</div>

        <script>
            let uid = '<?php echo $user_info['message']['user_id'];?>';
            let apiType = '';
            let page = 1;
            let keyword = '';
            let note_type = 3;
        </script>
        <script src="/static/js/jquery.min.js"></script>
        <script src="/static/js/message.min.js"></script>
        <script src="/static/js/custom.js"></script>
        <script src="/static/js/masonry.pkgd.min.js"></script>
        <script src="/static/js/imagesloaded.pkgd.min.js"></script>
        <script>
                // 计算notify宽度定义一个函数来获取宽度并设置到 .notify_head 上
                function updateNotifyHeadWidth() {
                    // 获取 .notify_comments 元素的宽度
                    const notifyCommentsElement = document.querySelector('.notify_comments');
                    const notifyCommentsWidth = notifyCommentsElement ? notifyCommentsElement.offsetWidth : 0;

                    // 将计算出的宽度设置到 .notify_head 元素上
                    const notifyHead = document.querySelector('.notify_head');
                    if (notifyHead) {
                        notifyHead.style.width = notifyCommentsWidth + 10 + 'px';
                    }
                }

                window.onload = updateNotifyHeadWidth;
                window.onresize = updateNotifyHeadWidth;
                //ajax fans
                $('.follows_btn, .becomefans').click(function() {
                    var $this = $(this);
                    var fans_id = $(this).data('fans-id'); // 获取粉丝ID
                    var uid = $(this).data('uid'); // 获取用户ID

                    $.ajax({
                        url: '/api/action.php?type=fans',
                        method: 'POST',
                        data: {
                            fans_id: fans_id,
                            uid: uid
                        },
                        dataType: 'json',
                        success: function(response) {
                            // 处理响应
                            if (response.status === '200') {
                                Qmsg.success(response.message);
                                $this.addClass('follows_btn');
                                $this.text('已关注'); // 更新按钮文本
                            } else {
                                Qmsg.info(response.message);
                                $this.removeClass('follows_btn'); // 移除已关注的类
                                $this.text('关注'); // 更新按钮文本
                                //alert('操作失败: ' + response.message);
                            }
                        },
                        error: function(xhr, status, error) {
                            // 处理错误
                            Qmsg.error(error);
                            //alert('请求失败: ' + error);
                        }
                    });
                });
        </script>
	</body>
</html>